package com.it.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Studentsystem {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        loop:while (true) {
            System.out.println("------欢迎来到学生管理系统------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出学生");
            System.out.println("请输入你的选择:");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> findStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    break loop;
                    //给while循环命名，跳出loop这个循环，而不是单单
                    //只跳出switch循环
                    //第二种方法System.exit(0)
                    // 停止虚拟机运行
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        //利用空参构造创建学生对象
        Student s = new Student();

        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入学生id");
            //String id = sc.next();
            id = sc.next();
            boolean flag = contains(list, id);
            if(flag) {
                System.out.println("输入的学生对象重复");
            }else{
                s.setId(id);
                break;
            }
        }
        //调用 Scanner 对象的 next() 方法，读取用户输入的下一个字符串
        //输入的字符串被存储在变量 id 中
        //s.setId(id)
        //调用 Student 对象 s 的 setId 方法，
        // 将用户输入的 id 值设置为该学生对象的 id 属性

        System.out.println("请输入学生姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生地址");
        String address = sc.next();
        s.setAddress(address);

        //将学生信息存入list里
        list.add(s);

        //提醒学生信息录入成功
        System.out.println("学生信息录入成功");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        //System.out.println("删除学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        int index = getIndex(list, id);
        if(index >= 0) {
            list.remove(index);
            System.out.println("删除成功");
        }else{
            System.out.println("要删除的学生id不存在");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        //System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生id");
        String id = sc.next();

        //判断id是否存在
        int index = getIndex(list, id);
        if(index == -1) {
            System.out.println("该学生id不存在");
            return;
        }

        //获取要修改的学生对象
        Student stu = list.get(index);

        System.out.println("请输入要修改的学生姓名");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请输入要修改的学生年龄");
        int newAge = sc.nextInt();
        stu.setAge(newAge);

        System.out.println("请输入要修改学生的地址");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        System.out.println("修改成功");

    }

    //查询学生
    public static void findStudent(ArrayList<Student> list) {
        if(list.size() == 0){
            System.out.println("没有学生成员对象");
            return;
        }

        //打印表头
        System.out.println("id\t\tname\tage\taddress");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }

    //判断输入的学生对象是否存在
    public static boolean contains(ArrayList<Student> list,String id){
        //循环遍历每一个集合对象
        /*for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                return true;
            }
        }
        return false;*/
        return getIndex(list,id) >= 0;
    }

    //通过id获得索引的方法
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                return i;
            }
        }
        return -1;
    }
}
